<template>
    <h1>登录页</h1>
    <el-dropdown>
	    <span class="el-dropdown-link">
	      Dropdown
	    </span>
	    <template #dropdown>
	      <el-dropdown-menu>
	        <el-dropdown-item @click="changeZh">{{ $t('login.languageZh') }}</el-dropdown-item>
	        <el-dropdown-item @click="changeEn">{{ $t('login.languageEn') }}</el-dropdown-item>
	      </el-dropdown-menu>
	    </template>
	  </el-dropdown>
    <el-form :model="FormList" ref="myForm" :rules = "rules">
        <el-form-item  :label="$t('login.username')" prop="username">
            <el-input v-model="FormList.username" :placeholder="$t('login.userPlaceholder')"></el-input>
        </el-form-item>
        <el-form-item :label="$t('login.password')" prop="pwd">
            <el-input  v-model="FormList.pwd"></el-input>
        </el-form-item>
        <el-form-item >
           <el-button @click = "go(myForm)">{{$t('login.loginBtn')}}</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup lang="ts">
    import axios from "axios"
    import { reactive ,ref,getCurrentInstance} from 'vue'
    import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
    import {Login} from "@/api";
    import type {loginResponseType} from "@/model/user"
     // 语言包
     console.log(getCurrentInstance)

    //getCurrentInstance函数获取当前组件实例,并通过.appContext.config.globalProperties访问全局属性
    //?号表示前面如果返回null，后面不执行
    let $this = getCurrentInstance()?.appContext.config.globalProperties as any
    console.log($this)
    //切换中文，存储语言状态
    const changeZh = () => {
    $this.$i18n.locale = 'zh'
        console.log($this.$i18n)
        localStorage.setItem('lang', 'zh')
    }
    //切换英文，存储语言状态 
    const changeEn = () => {
        $this.$i18n.locale = 'en'
        localStorage.setItem('lang', 'en')
    }
    let myForm = ref<null|FormInstance>(null);
    interface FormListType{
        username:string,
        pwd:string
    }
    let FormList = reactive<FormListType>({
        username:"",
        pwd:""
    })
    // 自定义的验证
    let pwd = (rule: any, value: any, callback: any) => {
        let reg = /[0-9]{1,}/g
        if (value === '') {
            callback(new Error("请输入密码"))
        } else if (!reg.test(value)){
            callback(new Error("密码不对"))
        } else {
            callback()
        }
    }
    let rules = reactive<FormRules>({
        username: [
            { required: true, message: 'Please input Activity name', trigger: 'blur' }
        ],
        pwd:[{ validator: pwd, trigger: 'blur' }]
    
    })
    let go = (formEl: FormInstance|any)=>{
        formEl.validate(async (valid:boolean) => {
            if (valid) {
                console.log(valid);
                // 调接口
                // http://localhost:8080/SchoolSys/user/login
                // var baseUrl:String = "https://www.hotspotin.top/"
                // interface loginRequest{
                //     userName:String,
                //     pwd:String
                // }
                let data={
                    userName:"admin",
                    pwd:"admin123"
                }
                let res:loginResponseType | undefined = await Login(data) ;
                console.log("邓丽君",res?.user);

                
                // axios.post(baseUrl+"SchoolSys/user/login",data).then((res:any)=>{
                //     console.log(res);
                // })
            } 
        })
    }
</script>

<style scoped>

</style>